package com.qinhailin.common.util;

import com.qinhailin.common.config.WebContant;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Cmd {
	private static BufferedReader br;

	public static void execCommand(String arstringCommand) {
		try {
			Process p = Runtime.getRuntime().exec(arstringCommand,null,null);
			 //取得命令结果的输出流
            //输出流
            InputStream fis=p.getInputStream();
            //错误流
            InputStream ferrs=p.getErrorStream();
            //用一个读输出流类去读
            InputStreamReader isr=new InputStreamReader(fis);
            InputStreamReader errsr=new InputStreamReader(ferrs);
            //用缓冲器读行
            BufferedReader br=new BufferedReader(isr);
            BufferedReader errbr=new BufferedReader(errsr);
            String line=null;
            String lineerr = null;
            //直到读完为止
            while((line=br.readLine())!=null) {
            //有可能发生阻塞的问题
                    System.out.println("return input Str:" + line);
            }
            while((lineerr=errbr.readLine())!=null){
            //有可能发生阻塞的问题
                    System.out.println("return err Str:" + lineerr);
            }
            int exitVal = p.waitFor();
            System.out.println("exitVal:" + exitVal);


			p.waitFor();
			
			//p.destroy();
			System.out.println("我执行完了");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

//	String cmdcpfile = "cmd.exe java -jar d "+ WebContant.apktoolWorksspace + File.separator + requestip + File.separator +"20102015051700004.apk "
//			+WebContant.apktoolWorksspace + File.separator + requestip + File.separator+"20102015051700004" ;

	public static void execCommand(String arstringCommand,String dirroot) {
		System.out.println(arstringCommand);
		try {
			Process p = Runtime.getRuntime().exec(arstringCommand,null,new File(dirroot));
			//取得命令结果的输出流
			//输出流
			InputStream fis=p.getInputStream();
			//错误流
			InputStream ferrs=p.getErrorStream();
			//用一个读输出流类去读
			InputStreamReader isr=new InputStreamReader(fis);
			InputStreamReader errsr=new InputStreamReader(ferrs);
			//用缓冲器读行
			BufferedReader br=new BufferedReader(isr);
			BufferedReader errbr=new BufferedReader(errsr);
			String line=null;
			String lineerr = null;
			//直到读完为止
			while((line=br.readLine())!=null) {
				//有可能发生阻塞的问题
				System.out.println("return input Str:" + line);
			}
			while((lineerr=errbr.readLine())!=null){
				//有可能发生阻塞的问题
				System.out.println("return err Str:" + lineerr);
			}
			int exitVal = p.waitFor();
			System.out.println("exitVal:" + exitVal);


			p.waitFor();

			//p.destroy();
			System.out.println("我执行完了");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * 解压对应路径下的apk包
	 *
	 */
	public static void qianming(String keystroepath,String key_password,String outputpath,String outputapkname,String appPath,String key_bieming) {
		
		
		String sign_cmd = "jarsigner -keystore " + keystroepath
				+ " -storepass " + key_password + " -keypass " + key_password
				+ " -signedjar " + outputpath + "" + outputapkname + ".apk "
				+ appPath + " " + key_bieming;
		
		System.out.println(sign_cmd);
		execCommand(sign_cmd);
	}
	
	


}
